package com.yztc.orangeentertainment.ui.main;

import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.lidroid.xutils.DbUtils;
import com.yztc.orangeentertainment.App;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.base.BaseActivity;
import com.yztc.orangeentertainment.ui.main.channalfragment.ChannalFragment;
import com.yztc.orangeentertainment.ui.main.findfragment.findmain.FindFragment;
import com.yztc.orangeentertainment.ui.main.minefragment.MineFragment;
import com.yztc.orangeentertainment.ui.main.subscribefragment.SubscribeFragment;

import butterknife.InjectView;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {


    @InjectView(R.id.layout)
    LinearLayout layout;
    @InjectView(R.id.rb_channal)
    RadioButton rbChannal;
    @InjectView(R.id.rb_sub)
    RadioButton rbSub;
    @InjectView(R.id.rb_find)
    RadioButton rbFind;
    @InjectView(R.id.rb_mine)
    RadioButton rbMine;
    @InjectView(R.id.rg)
    RadioGroup rg;
    public static DbUtils dbUtils;
    private ChannalFragment channalFragment;
    private SubscribeFragment
            subscribeFragment;
    private FindFragment findFragment;
    private MineFragment mineFragment;
    private Bundle bundle;
    private int tag;
    private String id;
    private Long fristTime = (long) 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbUtils = DbUtils.create(MainActivity.this, "shoucang.db");
        //  加载VideoView的布局 或者 播放视频前 屏幕可能闪过黑屏问题解决
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        //初始化Fresco
        Fresco.initialize(this);

        initView();
        App.addActivity(this);
        //radiogroup监听事件 默认选中第一个
        rg.setOnCheckedChangeListener(this);
        rbChannal.setTextColor(Color.BLACK);
        rbSub.setTextColor(Color.GRAY);
        rbFind.setTextColor(Color.GRAY);
        rbMine.setTextColor(Color.GRAY);
        tag = getIntent().getIntExtra("tag", 0);
        if (tag == 1) {
            id = getIntent().getStringExtra("id");
            Setselection(3);
            rbMine.setChecked(true);
        } else {
            id = "0";
            Setselection(0);
        }
    }

    private void initView() {
        //动态修改RadioButton大小
        RadioButton[] rbs = {rbChannal, rbSub, rbFind, rbMine};
        for (RadioButton rb : rbs) {
            //挨着给每个RadioButton加入drawable限制边距以控制显示大小
            Drawable[] drs = rb.getCompoundDrawables();
            //获取drawables
            Rect r = new Rect(0, 0, drs[1].getMinimumWidth() * 1 / 2, drs[1].getMinimumHeight() * 1 / 2);
            //定义一个Rect边界
            drs[1].setBounds(r);
            //给drawable设置边界

            rb.setCompoundDrawables(null, drs[1], null, null);
            //添加限制给控件
        }
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    private void Setselection(int index) {
        //FragmentTransaction对fragment进行添加，删除，替换，等其他执行操作
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hindFragment(ft);
        //需要加载的页面
        switch (index) {
            case 0:
                //频道
                if (channalFragment == null) {
                    channalFragment = new ChannalFragment();
                    ft.add(R.id.layout, channalFragment);
                } else {
                    ft.show(channalFragment);
                }
                break;
            case 1:
                //订阅
                if (subscribeFragment == null) {
                    subscribeFragment = new SubscribeFragment();
                    ft.add(R.id.layout, subscribeFragment);
                } else {
                    ft.show(subscribeFragment);
                }
                break;
            case 2:
                //发现
                if (findFragment == null) {
                    findFragment = new FindFragment();
                    ft.add(R.id.layout, findFragment);
                } else {
                    ft.show(findFragment);
                }
                break;
            case 3:
                //我的
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    bundle = new Bundle();
                    bundle.putString("id", id);
                    mineFragment.setArguments(bundle);
                    ft.add(R.id.layout, mineFragment);
                } else {
                    ft.show(mineFragment);
                }
                break;

        }
        ft.commit();
    }

    private void hindFragment(FragmentTransaction ft) {
        //如果fragment不为空则保存当前fragment状态
        if (channalFragment != null) {
            ft.hide(channalFragment);
        }
        if (subscribeFragment != null) {
            ft.hide(subscribeFragment);
        }
        if (findFragment != null) {
            ft.hide(findFragment);
        }
        if (mineFragment != null) {
            ft.hide(mineFragment);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            //频道
            case R.id.rb_channal:
                rbChannal.setTextColor(Color.BLACK);
                rbSub.setTextColor(Color.GRAY);
                rbFind.setTextColor(Color.GRAY);
                rbMine.setTextColor(Color.GRAY);
                Setselection(0);
                break;
            //订阅
            case R.id.rb_sub:
                rbChannal.setTextColor(Color.GRAY);
                rbSub.setTextColor(Color.BLACK);
                rbFind.setTextColor(Color.GRAY);
                rbMine.setTextColor(Color.GRAY);
                Setselection(1);
                break;
            //发现
            case R.id.rb_find:
                rbChannal.setTextColor(Color.GRAY);
                rbSub.setTextColor(Color.GRAY);
                rbFind.setTextColor(Color.BLACK);
                rbMine.setTextColor(Color.GRAY);
                Setselection(2);
                break;
            //我的
            case R.id.rb_mine:
                rbChannal.setTextColor(Color.GRAY);
                rbSub.setTextColor(Color.GRAY);
                rbFind.setTextColor(Color.GRAY);
                rbMine.setTextColor(Color.BLACK);
                Setselection(3);
                break;
        }
    }

    @Override
    //按两次结束
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - fristTime > 2000) {
                Toast.makeText(MainActivity.this, "请在按一次退出", Toast.LENGTH_SHORT).show();
                fristTime = System.currentTimeMillis();
                return false;
            } else {
                finish();
                System.exit(keyCode);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
